package com.kfm.springboot.controller;

import cn.hutool.core.lang.tree.Tree;
import com.kfm.springboot.domain.SysMenu;
import com.kfm.springboot.domain.SysUser;
import com.kfm.springboot.domain.TreeMenu;
import com.kfm.springboot.service.IndexService;
import com.kfm.springboot.service.SysUserService;
import com.kfm.springboot.util.Contains;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private IndexService indexService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping({"/", "/login"})
    public ModelAndView login(){
        log.info("访问登录页面");
        return new ModelAndView("login");
    }

    @PostMapping("/login")
    public ModelAndView doLogin(SysUser sysUser, HttpSession session){
        ModelAndView mv = new ModelAndView();
        SysUser login = sysUserService.login(sysUser);
        if (login != null){
            // 登录成功
            session.setAttribute(Contains.LOGIN_FLAG, login);
            mv.setViewName("redirect:index");
        } else {
            // 登录失败
            mv.addObject("msg", "用户名或密码密码错误");
        }

        return mv;
    }

    @GetMapping("/index")
    public ModelAndView index(ModelAndView mv, HttpSession session){
        mv.setViewName("index");
        Collection<TreeMenu> menuByUser = null;
        // 根据登录用户， 显示菜单
        SysUser sysUser = (SysUser) session.getAttribute(Contains.LOGIN_FLAG);
        // 如果查询过
        if (redisTemplate.hasKey(Contains.REDIS_MENU_KEY + "_" + sysUser.getId())){
            Object o = redisTemplate.opsForValue().get(Contains.REDIS_MENU_KEY + "_" + sysUser.getId());
            menuByUser = (Collection<TreeMenu>) o;
        } else{

            menuByUser = indexService.getMenuByUser(sysUser.getId());
            // 将菜单存入 redis， 过期时间为 3600 秒
            redisTemplate.opsForValue().set(Contains.REDIS_MENU_KEY + "_" + sysUser.getId(), menuByUser, 3600, TimeUnit.SECONDS);
        }

//        List<Tree<Integer>> menuByUser = indexService.getMenuByUser2(sysUser.getId());
        mv.addObject("menus", menuByUser);
        return mv;
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpSession session){
        ModelAndView modelAndView = new ModelAndView("redirect:login");

        SysUser sysUser = (SysUser) session.getAttribute(Contains.LOGIN_FLAG);
        redisTemplate.delete(Contains.REDIS_MENU_KEY + "_" + sysUser.getId());

        session.invalidate();
        return modelAndView;
    }
}
